tongfusoft.inputProfile = function() {
}
tongfusoft.inputProfile.prototype = {
	// 列表数据
	initTable : function() {

		var table = new tongfusoft.table();

		var columns = [
				{
					"data" : "profileId",
				},
				{
					"data" : "profileId",
					"render" : function(data, type, full, meta) {
						var profileId = '<input type="hidden" class="profileIdData" name="profileIdData" value="'
								+ data + '" />'
						return profileId;
					}
				},
				{
					"data" : "profileName"
				},
				{
					"data" : "profileNote"
				},
				{
					"data" : "loginName"
				},
				{
					"data" : "lastUpdDate"
				},
				{
					"data" : "isEnable",
					"render" : function(data, type, full, meta) {
						if (data == '1') {
							var isEnable = '<label class="switch ">'
									+ '<input type="checkbox" checked="checked" onclick="new tongfusoft.inputProfile().switchIsEnable(this)">'
									+ '<span></span>'
									+ '</label>'
									+ '<input type="hidden" class="isEnable" name="isEnable" value="'
									+ data + '" />'
							return isEnable;
						} else {
							var isEnable = '<label class="switch ">'
									+ '<input type="checkbox" onclick="new tongfusoft.inputProfile().switchIsEnable(this)">'
									+ '<span></span>'
									+ '</label>'
									+ '<input type="hidden" class="isEnable" name="isEnable" value="'
									+ data + '" />'
							return isEnable;
						}
					}
				},
				{
					"data" : "isEnable",
					"render" : function(data, type, full, meta) {
						if (data == '0') {
							var detailbut = '<button '
									+ '" onclick="new tongfusoft.inputProfile().detail(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="icon-list"></em></div></button>'
							var delbut = '<button profileId="'
									+ data
									+ '" onclick="new tongfusoft.inputProfile().del(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
							var editbut = '<button profileId="'
									+ data
									+ '" onclick="new tongfusoft.inputProfile().editPage(this)" type="button" data-toggle="modal" class="ecitBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'
									+ '<input type="hidden" class="profileIdData" name="profileIdData" value="'
									+ data + '" />'
							return detailbut + editbut + delbut;
						} else {
							var detailbut = '<button '
									+ '" onclick="new tongfusoft.inputProfile().detail(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="icon-list"></em></div></button>'
							var hidden = '<input type="hidden"  value="' + data + '" />'
							var delbut = '<button profileId="'
								+ data
								+ '" onclick="new tongfusoft.inputProfile().del(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
							var editbut = '<button profileId="'
								+ data
								+ '" onclick="new tongfusoft.inputProfile().editPage(this)" type="button" data-toggle="modal" class="ecitBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'
								+ '<input type="hidden" class="profileIdData" name="profileIdData" value="'
								+ data + '" />'

								return detailbut + editbut + delbut;
						}
					}
				} ]

		table.dataTable('inputProfiletTable', 'inputProfile/query', columns, null);
	},

	// 添加模板
	createPage : function() {
		$("#addBtn").click(function() {
			$("#modelTitle").text('新增模板');
			common.clean("addInputProfileForm");
			$("#profileNote").val("");
			$('#dialogMode').modal('show');

		})

	},
	// 详情
	detail : function(obj) {
		var profileId = $(obj).parents("tr").find(".profileIdData").val();

		window.location.href = common.getContextPath('manager/inputProfile/inputProfileDetail/'+ profileId);

	},

	// 提交数据
	dataSubmit : function() {

		$.validator
				.setDefaults({
					submitHandler : function() {
						var initData = common.getData("addInputProfileForm");
						initData['profileNote'] = $("#profileNote").val();
						var url = 'inputProfile/add';

						common.ajaxSubmitOnSuccess( initData, url, function(data) {

						 if (data.state == 'success') {
							 
							toastr["success"]('添加成功!');
							$('#dialogMode').modal('hide');
							
							window.location.href = common.getContextPath('manager/inputProfile/fieldset/'+ data.data);

						} else if (data.code == 'repeat_error') {
							toastr["error"]('模板名称不能重复!');
						} else{
							toastr["error"]('保存失败!');
						}
						}, $("#oper").val())
					}
				});
		$("#addInputProfileForm").validate();

		$("#submitBtn").click(function() {
			$("#addInputProfileForm").submit();

		})
	},
	// 删除
	del : function(obj) {
		var profileId = $(obj).parents("tr").find(".profileIdData").val();
		swal({
			title : "是否确认?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#DD6B55",
			confirmButtonText : "删除",
			cancelButtonText : "取消",
			closeOnConfirm : true
		}, function() {
			common.ajaxSubmitOnSuccess({
				"profileId" : profileId
			}, 'inputProfile/del', function(data) {

				if (data.state == 'success') {

					$('#inputProfiletTable').DataTable().ajax.reload();
				}

			}, "del")
		});

	},
	// 
	editPage : function(obj) {
		var profileId = $(obj).parents("tr").find(".profileIdData").val();
		window.location.href = common.getContextPath('manager/inputProfile/updateInputProfile/' + profileId);
		

	},

	// 启用模板
	switchIsEnable : function(obj) {
		var profileId = $(obj).parents("tr").find(".profileIdData").val();
		var hasIsEnable = $(obj).parents("tr").find(".isEnable").val();
		var isEnable = "";
		if (hasIsEnable === '0') {
			isEnable = '1';
		} else {
			isEnable = '0';
		}
		common.ajaxSubmitOnSuccess({
			"profileId" : profileId,
			"isEnable" : isEnable
		}, 'inputProfile/isEnable', function(data) {

			if (data.state == 'success') {
				toastr["success"]('切换成功!');
				$('#inputProfiletTable').DataTable().ajax.reload();
			}else{
				toastr["success"]('切换失败!');
			}
		})

	},
	search : function() {

		$("#searchBtn").click(function() {

			$('#inputProfiletTable').DataTable().ajax.reload();

		})

	}

}

$(function() {
	var obj = new tongfusoft.inputProfile();
	obj.initTable();
	obj.createPage();
	obj.dataSubmit();
	obj.search();
})